package com.qvtu.springboot.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qvtu.springboot.entity.Article;
import com.qvtu.springboot.mapper.ArticleMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleService {

    @Resource
    private ArticleMapper articleMapper;


    public void add(Article aritcle) {
        aritcle.setTime(DateUtil.now());//获取当前最新的时间字符串
        articleMapper.insert(aritcle);
    }

    public void delete(Integer id) {
        articleMapper.delete(id);
    }

    public void update(Article article) {
        articleMapper.update(article);
    }

    public Article selectById(Integer id) {
        Article article= articleMapper.selectById(id);
        return article;
    }

    public List<Article> selectAll(Article article) {
        List<Article> list=articleMapper.selectAll(article);
        return list;
    }

    //    分页查询
    public PageInfo<Article> selectPage(Article article, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> list = articleMapper.selectAll(article);
        return PageInfo.of(list);
    }

    public void deleteBatch(List<Integer> ids) {
        for(Integer i: ids){
            this.delete(i);
        }
    }

    public Integer selectCountByDate(String day) {
        return articleMapper.selectCountByDate(day);
    }
}
